package handler

import (
	"net/http"
	"strconv"

	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/service"
	"fitness/go-admin/pkg/response"

	"github.com/gin-gonic/gin"
)

// ReviewHandler 评价处理器
type ReviewHandler struct {
	reviewService service.ReviewService
}

// NewReviewHandler 创建评价处理器实例
func NewReviewHandler(reviewService service.ReviewService) *ReviewHandler {
	return &ReviewHandler{
		reviewService: reviewService,
	}
}

// ==================== 课程评价 ====================

// CreateCourseReview 创建课程评价
func (h *ReviewHandler) CreateCourseReview(c *gin.Context) {
	var req model.CreateCourseReviewReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取用户ID（实际项目中从token解析）
	userID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.CreateCourseReview(userID, &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "评价成功")
}

// GetCourseReviews 获取课程评价列表
func (h *ReviewHandler) GetCourseReviews(c *gin.Context) {
	courseID, err := strconv.ParseUint(c.Param("courseId"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "课程ID格式错误")
		return
	}

	var req model.CourseReviewListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	req.CourseID = uint(courseID)

	// 从上下文中获取当前用户ID
	currentUserID := uint(1) // 示例值，实际应从token中获取

	resp, err := h.reviewService.GetCourseReviews(uint(courseID), &req, currentUserID)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// MarkReviewHelpful 标记评价有用
func (h *ReviewHandler) MarkReviewHelpful(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.MarkReviewHelpful(userID, uint(reviewID)); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "标记成功")
}

// UnmarkReviewHelpful 取消标记评价有用
func (h *ReviewHandler) UnmarkReviewHelpful(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.UnmarkReviewHelpful(userID, uint(reviewID)); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "取消标记成功")
}

// ReplyToReview 回复评价
func (h *ReviewHandler) ReplyToReview(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	var req struct {
		Content string `json:"content" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取用户ID和用户类型
	userID := uint(1)    // 示例值，实际应从token中获取
	userType := "MEMBER" // 示例值，实际应从token中获取

	// 从URL参数获取评价类型
	reviewType := c.Param("type") // "course" 或 "coach"
	var actualReviewType string
	if reviewType == "course" {
		actualReviewType = "COURSE"
	} else if reviewType == "coach" {
		actualReviewType = "COACH"
	} else {
		response.Error(c, http.StatusBadRequest, "评价类型错误")
		return
	}

	if err := h.reviewService.ReplyToReview(userID, userType, uint(reviewID), actualReviewType, req.Content); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "回复成功")
}

// ==================== 教练评分 ====================

// CreateCoachRating 评价教练
func (h *ReviewHandler) CreateCoachRating(c *gin.Context) {
	var req model.CreateCoachRatingReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	resp, err := h.reviewService.CreateCoachRating(userID, &req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, resp, "评价成功")
}

// GetCoachRatings 获取教练评分
func (h *ReviewHandler) GetCoachRatings(c *gin.Context) {
	coachID, err := strconv.ParseUint(c.Param("coachId"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "教练ID格式错误")
		return
	}

	var req model.CoachRatingListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	req.CoachID = uint(coachID)

	resp, err := h.reviewService.GetCoachRatings(uint(coachID), &req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// ==================== 意见反馈 ====================

// CreateFeedback 提交反馈
func (h *ReviewHandler) CreateFeedback(c *gin.Context) {
	var req model.CreateFeedbackReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.CreateFeedback(userID, &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "感谢您的反馈，我们会尽快处理")
}

// GetMyFeedbacks 获取我的反馈列表
func (h *ReviewHandler) GetMyFeedbacks(c *gin.Context) {
	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	feedbacks, err := h.reviewService.GetMyFeedbacks(userID)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, feedbacks, "获取成功")
}

// GetAllFeedbacks 获取所有反馈（管理员）
func (h *ReviewHandler) GetAllFeedbacks(c *gin.Context) {
	var req model.FeedbackListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	resp, err := h.reviewService.GetAllFeedbacks(&req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// HandleFeedback 处理反馈（管理员）
func (h *ReviewHandler) HandleFeedback(c *gin.Context) {
	feedbackID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "反馈ID格式错误")
		return
	}

	var req model.HandleFeedbackReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取处理人ID
	handlerID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.HandleFeedback(handlerID, uint(feedbackID), &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "处理成功")
}

// ==================== 管理员功能 ====================

// AuditReview 审核评价
func (h *ReviewHandler) AuditReview(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	var req model.AuditReviewReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取管理员ID
	adminID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.AuditReview(adminID, uint(reviewID), &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "审核成功")
}

// FeatureReview 设置精选评价
func (h *ReviewHandler) FeatureReview(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	var req model.FeatureReviewReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取管理员ID
	adminID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.FeatureReview(adminID, uint(reviewID), &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "设置成功")
}

// HideReview 隐藏评价
func (h *ReviewHandler) HideReview(c *gin.Context) {
	reviewID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "评价ID格式错误")
		return
	}

	// 从上下文中获取管理员ID
	adminID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.HideReview(adminID, uint(reviewID)); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "隐藏成功")
}

// ==================== 评价举报管理 ====================

// ReportReview 举报评价
func (h *ReviewHandler) ReportReview(c *gin.Context) {
	var req model.ReportReviewReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取用户ID
	userID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.ReportReview(userID, &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "举报成功")
}

// GetReviewReports 获取举报列表（管理员）
func (h *ReviewHandler) GetReviewReports(c *gin.Context) {
	var req model.ReviewReportListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	resp, err := h.reviewService.GetReviewReports(&req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// HandleReviewReport 处理举报（管理员）
func (h *ReviewHandler) HandleReviewReport(c *gin.Context) {
	reportID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "举报ID格式错误")
		return
	}

	var req model.HandleReportReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误: "+err.Error())
		return
	}

	// 从上下文中获取管理员ID
	adminID := uint(1) // 示例值，实际应从token中获取

	if err := h.reviewService.HandleReviewReport(adminID, uint(reportID), &req); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, nil, "处理成功")
}
